package org.iteam.xlz.core.service.message;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.json.JSONArray;
import org.iteam.commons.json.JSONException;
import org.iteam.commons.json.JSONObject;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.messages.Feedback;

public class FeedbackService extends BaseService<Feedback>{
	
	static FeedbackService instance;
	
	private FeedbackService(){}
	
	public static FeedbackService getInstance(){
		if(null==instance){
			instance=new FeedbackService();
		}
		return instance;
	}
	
	public Feedback find(String fid){
		try {
			return super.show("select * from feedback where fid=?",fid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Feedback();
		}
	}
	
	public long counts(String mobile,String leftCreateTime,String rightCreateTime,String type,String tid){
		try {
			List<Object> params=new ArrayList<Object>();
			StringBuilder sql=new StringBuilder("select count(1) from feedback l left join app_user r on l.uid=r.uid where 1=1");
			if(StringUtils.isNotBlank(mobile)){
				sql.append(" and r.phone like ?");
				params.add("%"+mobile+"%");
			}
			if(StringUtils.isNotBlank(leftCreateTime)&&StringUtils.isNotBlank(rightCreateTime)){
				sql.append(" and l.createTime between ? and ?");
				params.add(leftCreateTime);
				params.add(rightCreateTime);
			}
			if(StringUtils.isNotBlank(type)){
				sql.append(" and l.status=?");
				params.add(type);
			}
			if(StringUtils.isNotBlank(tid)){
				sql.append(" and l.tid=?");
				params.add(tid);
			}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Feedback> query(String mobile,String leftCreateTime,String rightCreateTime,String type,String tid,long page,long rows){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder("select l.fid,r.phone as uid,l.tid,l.feedbackcontent,l.createTime,"+
		"l.status,b.name as typeName from feedback l left join app_user r on l.uid=r.uid left join feedbacktype b on l.tid = b.tid where 1=1");
		if(StringUtils.isNotBlank(mobile)){
			sql.append(" and r.phone like ?");
			params.add("%"+mobile+"%");
		}
		if(StringUtils.isNotBlank(leftCreateTime)&&StringUtils.isNotBlank(rightCreateTime)){
			sql.append(" and l.createTime between ? and ?");
			params.add(leftCreateTime);
			params.add(rightCreateTime);
		}
		if(StringUtils.isNotBlank(type)){
			sql.append(" and l.status=?");
			params.add(type);
		}
		if(StringUtils.isNotBlank(tid)){
			sql.append(" and l.tid=?");
			params.add(tid);
		}
		sql.append(" order by l.createTime desc limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Feedback>();
		}
	}
	
	public Feedback getFeedbackById(String fid){
		try {
			Feedback feedback=super.show("select l.fid,r.phone as uid,l.tid,l.feedbackcontent,l.createTime,"+
			"l.status,l.feedImagUrls,b.name as typeName from feedback l left join app_user r on l.uid=r.uid left join feedbacktype b on l.tid = b.tid where 1=1 and l.fid=?" ,fid);
			if (null != feedback) {
				String json = feedback.getFeedImagUrls();
				if(StringUtils.isNotBlank(json)){
					try {
						JSONObject jsonObject = new JSONObject(json);
						JSONArray jsonArray = jsonObject.getJSONArray("feedImagUrls");
						for(int i=0;i<jsonArray.length();i++){
							feedback.getImagUlrs().add(jsonArray.optString(i));
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
				if("0".equals(feedback.getStatus())){
					feedback.setStatus("1");
					SqlBuilder builder=SqlBuilder.createUpdate("feedback");
					builder.addField("status","1");
					builder.addWhere("fid",fid);
					super.updates(builder.getSql(),builder.getParams());
				}
			} else {
				feedback = new Feedback();
			}
			return feedback;
		} catch (SQLException e) {
			e.printStackTrace();
			return new Feedback();
		}
	}
	
	public boolean delete(String fid){
		try {
			return super.updates("delete from feedback where fid=?",fid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
